package com.vochupin.bt;

import java.util.Set;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class BluetoothActivity extends Activity {
	
	private static final int REQUEST_ENABLE_BT = 1;
	private TextView tvLog;
	private Button btnStartDiscovery;
	private BluetoothAdapter btAdapter;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bluetooth);
        
        tvLog = (TextView)findViewById(R.id.tvBt);
        tvLog.setText("Log:\n");

        btnStartDiscovery = (Button)findViewById(R.id.btnStartDiscovery);
        btnStartDiscovery.setOnClickListener(startDiscoveryClick);

        initBluetooth();
    }
    
	@Override
	protected void onResume() {
		IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
		registerReceiver(discoveryReceiver, filter);
		super.onResume();
	}

	@Override
	protected void onPause() {
		unregisterReceiver(discoveryReceiver);
		super.onPause();
	}

	private void initBluetooth() {
		btAdapter = BluetoothAdapter.getDefaultAdapter();
        if (btAdapter == null) {
        	tvLog.append("Bluetooth absent.\n");
            btnStartDiscovery.setEnabled(false);
        	return;
        }
        
    	tvLog.append("Bluetooth present.\n");
    	if (!btAdapter.isEnabled()) {
            btnStartDiscovery.setEnabled(false);
    	    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    	    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    	}else{
			tvLog.append("Bluetooth already enabled.\n");
    	}
    }

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch(requestCode){
		case REQUEST_ENABLE_BT:
			tvLog.append("Bluetooth enabled.\n");
			btnStartDiscovery.setEnabled(true);
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	private OnClickListener startDiscoveryClick = new OnClickListener(){

		public void onClick(View v) {
			if(btAdapter != null){
				Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
				if (pairedDevices.size() > 0) {
				    for (BluetoothDevice device : pairedDevices) {
				    	tvLog.append("name:" + device.getName() + " MAC:" + device.getAddress() + "\n");
				    }
				}else{
					tvLog.append("No paired devices. Start discovery = " + btAdapter.startDiscovery());
				}
			}
		}
	};
	
	private final BroadcastReceiver discoveryReceiver = new BroadcastReceiver() {
	    public void onReceive(Context context, Intent intent) {
	        String action = intent.getAction();
	        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
	            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
	            tvLog.append("name:" + device.getName() + " MAC:" + device.getAddress() + "\n");
	        }
	    }
	};
}
